package model

import (
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
	"log"
	"myGame/src/Config"
	"time"
)

type Prop struct {
	Image    []*ebiten.Image
	LastTime time.Time
	Category int
	State    bool
	Width    float64
	Height   float64
	X        float64
	Y        float64
}

func NewProp(screenWidth, screenHeight int) *Prop {
	prop0, _, err := ebitenutil.NewImageFromFile("./src/images/prop/prop0.png")
	if err != nil {
		log.Fatal(err)
	}
	prop1, _, err := ebitenutil.NewImageFromFile("./src/images/prop/prop1.png")
	if err != nil {
		log.Fatal(err)
	}
	prop2, _, err := ebitenutil.NewImageFromFile("./src/images/prop/prop2.png")
	if err != nil {
		log.Fatal(err)
	}
	width, height := prop1.Size()

	prop := &Prop{
		Image:    []*ebiten.Image{prop0, prop1, prop2},
		LastTime: time.Now(),
		Category: 2,
		State:    false,
		Width:    float64(width),
		Height:   float64(height),
		X:        0,
		Y:        0,
	}
	return prop
}

// 单独书写一个声明值的方法，方便后续管理
func (prop *Prop) Draw(screen *ebiten.Image, cfg *Config.Config, r *Realm) {
	op := &ebiten.DrawImageOptions{}
	if time.Now().Sub(prop.LastTime).Milliseconds() >= 10000 {
		prop.LastTime = time.Now() //这样子就能让这个判断·10s只判断出一次，和这个上一给道具被接受时间无关系
		prop.Category = randGen.Intn(3)
		prop.X = float64(randGen.Intn(600)) + 900
		prop.Y = float64(randGen.Intn(300)) + 200
		prop.State = true
	}
	if prop.X <= 0 {
		prop.State = false
	}
	if prop.State {
		prop.X -= cfg.ObstacleSpeed
		op.GeoM.Translate(prop.X, prop.Y)
		screen.DrawImage(prop.Image[prop.Category], op)
	}

}
